import axios from "axios";
import config from "@/config/config";

const request = axios.create();
const mode = import.meta.env.MODE;
export function $post(url, data) {
  let baseURL = "";
  if (mode != "development") {
    if (config.isTest) {
      baseURL = config.baseURL.test;
    } else {
      baseURL = config.baseURL.build;
    }
  }
  return new Promise((resolve, reject) => {
    request({
      timeout: 10000,
      baseURL: baseURL,
      method: "post",
      url: mode == "development" ? "/api" + url : url,
      data,
      headers: {
        "Content-Type": "application/json",
        // "Accept-Token": "123", //根据项目设置token
      },
    })
      .then((res) => {
        //对请求结果进行统一拦截处理
        if (res.status == 200 && res.data.code == 2000) {
          resolve(res.data.data);
        } else {
          reject(res.data);
        }
      })
      .catch((err) => {
        console.log(err);
        reject(err);
      });
  });
}

export default request;
